Connection.cpp (1742B)
1 /* Copyright 1999, Brian J. Swetland. All rights reserved. 2 ** Distributed under the terms of the OpenBLT License 3 */ 4 5 #include <blt/Connection.h> 6 #include <blt/Message.h> 7 #include <blt/namer.h> 8 #include <blt/syscall.h> 9 10 #include <string.h> 11 12 using namespace BLT; 13 14 Connection::~Connection() 15 { 16 port_destroy(_local_port); 17 } 18 19 int 20 Connection::Send(const Message *msg) 21 { 22 msg_hdr_t mh; 23 const void *data; 24 uint32 len; 25 int res; 26 27 msg->GetPackedData(&data,&len); 28 29 mh.flags = 0; 30 mh.src = _local_port; 31 mh.dst = _remote_port; 32 mh.data = (void*) data; 33 mh.size = len; 34 35 res = old_port_send(&mh); 36 if(res != len) { 37 return res; 38 } else { 39 return 0; 40 } 41 } 42 43 int 44 Connection::Recv(Message *msg) 45 { 46 msg_hdr_t mh; 47 int res; 48 char buf[1024]; 49 50 mh.flags = 0; 51 mh.dst = _local_port; 52 mh.size = 1024; 53 mh.data = buf; 54 55 if((res = old_port_recv(&mh)) < 0) return res; 56 57 return msg->PutPackedData(buf,res,mh.src); 58 } 59 60 int 61 Connection::Call(const Message *send, Message *recv) 62 { 63 int res; 64 if((res = Send(send))) { 65 return res; 66 } else { 67 return Recv(recv); 68 } 69 } 70 71 Connection * 72 Connection::FindService(const char *name) 73 { 74 Connection *cnxn; 75 int port; 76 77 if(!strcmp(name,"namer")){ 78 port = NAMER_PORT; 79 } else { 80 port = namer_find(name, 0); 81 } 82 if(port < 1) return 0; 83 84 cnxn = new Connection(); 85 cnxn->_remote_port = port; 86 87 return cnxn; 88 } 89 90 Connection * 91 Connection::CreateService(const char *name) 92 { 93 Connection *cnxn = new Connection(); 94 95 if(!strcmp(name,"namer")){ 96 port_destroy(cnxn->_local_port); 97 cnxn->_local_port = NAMER_PORT; 98 } else { 99 if(namer_register(cnxn->_local_port, (char*) name)){ 100 delete cnxn; 101 cnxn = 0; 102 } 103 } 104 105 return cnxn; 106 } 107 108 109 Connection::Connection() 110 { 111 _local_port = port_create(0,"cnxn:local_port"); 112 _remote_port = 0; 113 }