暗黑世界 客户端 通信

SocketClient.h
#ifndef _CDATA_NETSOCKET_H_
#define _CDATA_NETSOCKET_H_

#ifdef CC_PLATFORM_WIN32

#include "pthread.h"
#include <Winsock2.h>
#include <Wininet.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <string.h>

#else

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>

#endif

#include <string.h>
#include <stdlib.h>
#include <queue>

#include "cocos2d.h"
#include "ByteBuffer.h"
const int	SocketClient_WAIT_CONNECT = 0;
const int	SocketClient_OK = 1;
const int	SocketClient_DESTROY = 2;


class Message;
class MessageHandler;

using namespace std;

class SocketClient{
public:
	int m_hSocket;

	char m_serverId;
	char m_clientId;
	string m_host;
	int m_iport;
    //static cocos2d::CCDictionary * m_dictionary;    //存放接收的数据
	vector<char> m_serverId2;
	vector<char> m_clientId2;
	vector<string> m_host2;
	vector<int> m_iport2;//链接服务器1失败后,如果服务器2存在将连服务器2
	
	//发送和接收缓冲区,发送缓冲区满的时候,会断开连接,并提示信号不好
	ByteBuffer m_cbRecvBuf;
	ByteBuffer m_cbSendBuf;
	
	//收到服务端消息
	queue<Message*> m_receivedMessageQueue;
	
	//需要发送到服务端的消息
	queue<Message*> m_sendMessageQueue;
	
	int m_iState;
	MessageHandler* m_pHandler;
	
	//接收线程
	bool m_bThreadRecvCreated;
	pthread_t pthread_t_receive;
	
	//发送线程
	bool m_bThreadSendCreated;
	pthread_t pthread_t_send;

	//pthread_mutex_t 互斥锁
	pthread_mutex_t m_thread_cond_mutex;
	pthread_cond_t m_threadCond;
	
    //聊天线程
    bool m_bThreadChatCreated;
    pthread_t pthread_t_chat;
    pthread_mutex_t m_thread_chat_mutex;
    //pthread_cond_t m_threadCond;
	//发送队列同步锁
	pthread_mutex_t m_sendqueue_mutex;
	
	bool m_isvalidSeq;
	long long m_sabcde[6];
	long long getSeq();
private:
	//连接服务器
	bool  connectServer();
	
	static void* ThreadReceiveMessage(void *p);
	static void* ThreadSendMessage(void *p);
	
public:
	SocketClient(String host, int port, byte clientId,
				 byte serverId,MessageHandler* netImpl);
	
	~SocketClient();
	void start();
	void stop(boolean b);
	
	bool isWaitConnect();
	//发送数据
	void sendMessage_(Message* msg,bool b);	
	
	Message* popReceivedMessage();
	Message* pickReceivedMessage();
	
	void pushReceivedMessage(Message* msg);
	
	MessageHandler* getHandler(){
        return  m_pHandler;
    }       
	
	void setHandler(MessageHandler* handler){
		this->m_pHandler = handler;
	}
	
    Message* constructMessage(const char* data,int commandId);
    static int bytesToInt(byte* data);
    static byte* intToByte(int i);
    void swhlie(int commandId);
};



class SocketServer{
public:
	bool isStartedServer;
    bool isExistOtherServer;
    bool isStartServerBindFail;
    
    int server_socket;    
   
    SocketServer( );
	~SocketServer();
    
	bool startServer();
    void killOtherServer();
    void killed();
};
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值